
#include "dlCommd.hh"
#include <string>
#include "demsStr.hh"
#include <vector>
#include "status.hh"
#include "jdkIns.hh"
# include "infoSys.hh"
#include <iostream>
# include <unistd.h>
#include "setgcc.hh"
#include "verrecord.hh"
#include "setpython.hh"
# include "virenv.hh"
# include <pwd.h>
#include "rungcc.hh"
#include <fstream>
namespace dShell
{
    int commd(std::string cmd, std::vector<std::string> params)
    {

        // 判断各种各样的命令, 然后送相应位置.
        if (cmd == "setenv")
        {
            // setenv 操作需要两个或0个参数, 两个参数情况分别是安装什么工具, 什么版本;
            

            if (params.size() == 2) // 两个参数的情况
            {
                std::string para1, para2;
                para1 = params[0];
                para2 = params[1];

                int retCode = envset(para1, para2);
                if (retCode != SUCCESS)
                {
                
                }
                else
                {
                    std::cout << "设置环境成功" << std::endl;
                }
            }
            else if (params.size() < 2 and params.size() > 0)
            {
                std::cout << "参数个数不正确" << std::endl;
            }
            else if (params.size() == 0)
            {
                envset();
            }
        }
        else if(cmd =="gcc")
        {
         std::string temp;
         for(int i=0;i<params.size();i++) 
         {
               temp=temp+params[i];
         }  
         changegcc(temp,rtgccver());
        }
         else if(cmd =="g++")
        {
         std::string temp;
         for(int i=0;i<params.size();i++) 
         {
               temp=temp+params[i];
         }  
         changegcc(temp,rtgppver());
        }

        else if (cmd == "python")
        {
            std::string version = getpyversion();
            if (params.size() == 0)
            { 
                //std::system("~/miniconda3/envs/p_v3.7/bin/python");
                std::system(("~/miniconda3/envs/p_v" + version + "/bin/python").c_str());
            }
            else if (params.size() == 1)
            {
                std::string pa = params[0];
                if (pa[0] >= '2' && pa[0] <= '3')
                {
                    std::system(("~/miniconda3/envs/p_v" + params[0] + "/bin/python").c_str());
                }
                else
                {
                    std::system(("~/miniconda3/envs/p_v" + version + "/bin/python " + pa).c_str());
                }
            }
            else if (params.size() == 2)
            {
                std::cout << "开始执行python程序" << std::endl;
                std::system(("~/miniconda3/envs/p_v" + params[0] + "/bin/python " + params[1]).c_str());
            }

            //std::system("~/miniconda3/envs/p_v3.5/bin/python /mnt/d/ZShare/dms/a.py");
            //std::system(("~/miniconda3/envs/p_v"+params[0]+"/bin/python").c_str());
        }
        else if (cmd == "run")
        {
            std::string location = params[0];
            
            if(location.substr(location.length() - 5, 5) == ".java")
            {
                std::string copy = "cp " + location + " .";
                int loc = location.rfind('/');
                std::string allname = location.substr(loc + 1, location.length() - 1);
                std::string filename = allname.substr(0, allname.length() - 5);
                std::string path = "./" + allname;

                if(access(path.c_str(), F_OK) != 0){
                    // 将所要编译的文件复制到当前路径下
                    std::system(copy.c_str()); 
                }

                // 在当前路径进行编译运行
                if (allname.length() > 5 && allname.substr(allname.length() - 5, allname.length() - 1) == ".java")
                {
                    std::string compile = "javac " + allname;
                    std::string exe = "java " + filename;
                    std::system(compile.c_str());
                    std::system(exe.c_str());
                }

                // 将拷贝过来的文件移动至D盘的javafile文件夹中
                std::system("mkdir /mnt/d/javafile 2> /dev/null");
                std::string file = "mv " + allname + " " + filename + ".class" + " /mnt/d/javafile";
                std::system(file.c_str());
            }

            if(location.substr(location.length() - 3, 3) == ".cc" or location.substr(location.length() - 2, 2) == ".c")
            {
            rungcc(location,rtgccver());
            }
            if(location.substr(location.length() - 4, 4) == ".cpp")
            {
            rungpp(location,rtgccver());
            }
            if(location.substr(location.length() - 3, 3) == ".py")
            {
                runpython(location);
            }
        }
//        创建虚拟环境, 虚拟环境名, 虚拟环境
//        虚拟环境创建命令:
//        crtenv -n envName -t developenv=version developenv2=version developenv3=version
//        -n 之后找一个, -t 之后找剩下的.直到遇到-n或者结尾
        else if (cmd == "crtenv")
        {
            if (params.size() < 4)
            {
                std::cout << "参数个数不正确" << std::endl;
            }
            else
            {
                // 找到 -n -t 存在的地方.
                int tpos, tcheck = -1; // tcheck 算保险子, 如果为-1表示没有-t选项.
                int npos, ncheck = -1; // ncheck 原理同 tcheck
                for(tpos = 0;tpos<params.size();tpos++)
                {
                    if (params[tpos] == "-t")
                    {
                        tcheck = tpos;
                        break;
                    }
                }
                for(npos = 0; npos<params.size(); npos++)
                {
                    if (params[npos] == "-n")
                    {
                        ncheck = npos;
                        break;
                    }
                }
                /* 找到 -n 紧接着的那个 */
                std::string npara = params[npos+1];
                if (npara.empty())
                {
                    std::cout << "没有环境名, 请检查输入" << std::endl;
                }
                /* 找到 -t 紧接着的那一串 做切片操作*/
                std::vector<std::string> tparas = {&params[tpos]+1, &params[params.size()]};
                Venv devEnv(npara, tparas);
                /* 分解包名和版本号 */
                devEnv.splNameVer();
                devEnv.instEnv();
                


            }
            
        }
        else if (cmd == "cd")
        {
            passwd *pwd;
            pwd = getpwuid(getuid());
            std::string pwname = pwd->pw_name;
            if (params.size() == 0) // 检查参数个数
            {
                chdir(("/home/"+pwname).c_str());
            }
            else if (params.size() == 1) // 用户制定了转到哪里
            {
                chdir(params[0].c_str());
            }
            else
            {
                std::printf("参数个数不正确\n");
            }
        }
        else if (cmd == "--help")
        {   
            std::ifstream read;
            if (params.size() == 0)
            {
                read.open("../help.txt", std::ios::in);
            }
            else
            {
                std::string cmds = params[0];
                // std::cout << cmds << std::endl;
                cmds.erase(0, cmds.find_first_not_of(" "));
                cmds.erase(cmds.find_last_not_of(" ") + 1);
                if (cmds == "setenv")
                {
                    read.open("../setenv.txt", std::ios::in);
                }
                else if (cmds == "run")
                {
                    read.open("../run.txt", std::ios::in);
                }
            }
            int line_num = 0;
            std::string lines[99];

            while (!read.eof())
            {
                std::string temp;
                getline(read, temp, '\n');
                lines[line_num] = temp;
                line_num++;
            }

            for (int i = 0; i < line_num;i++)
            {
                std::cout << lines[i] << std::endl;
            }

        }
        else if(cmd == "saveenv")
        {
            if(params.size() == 0)
            {
                std::cout << "请输入的时候添加 虚拟环境名 !!!!!!" << std::endl;
            }
            else
            {
                std::string virname = params[0];
                std::string username = getUsrNme();
                // std::string file_location = "/home/" + username + "/.data/dems/virenv/envLists.dms";
                std::string file_location = "../test.md"; //测试文件
                std::ifstream read(file_location, std::ios::in);
                std::string info = "";

                while (!read.eof())
                {
                    std::string temp;
                    getline(read, temp, '\n');
                    if (temp.find(virname, 0) != std::string::npos)
                    {
                        int index = temp.find(virname, 0);
                        info = temp.substr(index + virname.length());
                        // std::cout << info << std::endl;
                        break;
                    }
                }
                std::vector <std::string> cmdParam;
                cmdParam = strSplit(info, " ");

                for (int i = 0; i < cmdParam.size();i++)
                {
                    getsaveinfo(cmdParam[i], "py");
                    getsaveinfo(cmdParam[i], "jdk");
                    getsaveinfo(cmdParam[i], "gcc");
                    getsaveinfo(cmdParam[i], "g++");
                }

                if (info == "")
                {
                    std::cout << "输入的虚拟环境名有误！！！！！" << std::endl;
                }
            }
        }
        else if(cmd == "loadenv")
        {
            if(params.size() == 0)
            {
                std::cout << "请输入的时候添加 路径 !!!!!!" << std::endl;
            }
            else
            {
                std::string path = params[0];
                path.erase(0, path.find_first_not_of(" "));
                path.erase(path.find_last_not_of(" ") + 1);
                std::ifstream read("/home/", std::ios::in);

                int line_num = 0;
                std::string lines[99];

                while (!read.eof())
                {
                    std::string temp;
                    getline(read, temp, '\n');
                    lines[line_num] = temp;
                    line_num++;
                }

                for (int i = 0; i < line_num;i++)
                {
                    // std::cout << lines[i] << std::endl;
                    std::vector <std::string> cmdParam;
                    cmdParam = strSplit(lines[i], " ");
                    envset(cmdParam[0], cmdParam[1]);
                }
            }
        else if (cmd == "venvact")
        {
            if(params.size() == 0)
            {
                std::cout << "缺少环境名字, 请重新输入" << std::endl;
            }
            else if (params.size() > 1)
            {
                std::cout << "环境名字多余, 请重新输入" << std::endl;
            }
            //  执行activate 的操作
            std::cout << params[0] << std::endl;
            actiVenv(params[0]);
            

        }
        else
        {
            std::string paramStr;
            for (std::string temp : params)
            {
                paramStr += temp;
                paramStr += " ";
            }
            // std::cout << "paramStr = " << paramStr << std::endl;
            std::system(("bash -c \"" + cmd + " " + paramStr + "\"").c_str());
        }
    }
    int envset(std::string para1, std::string para2)
    {
        if (para1 == "gcc")
        {
            gccverset(para2);
            int retcode =installgcc(rtgccver());
            if(retcode==FAIL)
            {
                std::cout << "安装失败, 请检查本软件是否完全安装, 或请反馈至官网: http://xxx.xxx.xxx.com/feedback"; 
            }
        }
        else if (para1 == "g++")
        {
            gppverset(para2);
            int retcode =installgcc(rtgppver());
            if(retcode==FAIL)
            {
                std::cout << "安装失败, 请检查本软件是否完全安装, 或请反馈至官网: http://xxx.xxx.xxx.com/feedback"; 
            }
        }
        else if (para1 == "java" || para1 == "jdk")
        {
            // std::cout << "para1 = " << para1 << std::endl;
            // return SUCCESS;
            if (para2 == "8" || para2 == "11" || para2 == "16")
            {
                int retcode = setupJDK(para2);
                if (retcode == FAIL)
                {
                    std::cout << "安装失败, 请检查本软件是否完全安装, 或请反馈至官网: http://xxx.xxx.xxx.com/feedback";
                }
            }
        }
        else if (para1 == "python")
        {
            /* 送python处理处 2.7~3.10*/
            if (para2 == "2.7" || para2 == "2.8" || para2 == "2.9" || para2 == "3.0" || para2 == "3.1" || para2 == "3.2" || para2 == "3.3" || para2 == "3.4" || para2 == "3.5" || para2 == "3.6" || para2 == "3.7" || para2 == "3.8" || para2 == "3.9" || para2 == "3.10")
            {
                int retcode = installpython(para2);
                if (retcode == FAIL)
                {
                    std::cout << "安装失败, 请检查本软件是否完全安装, 或请反馈至官网: http://xxx.xxx.xxx.com/feedback";
                }
            }
        }
        else
        {
            std::cout << "目前并没有这个开发环境的配置功能, 敬请期待" << std::endl;
            return FAIL;
        }
    }
    int envset()
    {
        std::vector<std::string> devEnv, devVers;
        std::vector<std::string> envNvers;
        std::string envName, envVersion;
        std::cout << "目前有如下几个开发环境以及版本可供选择:" << std::endl;
        std::cout << "GCC: 7, 8, 9, 10" << std::endl;
        std::cout << "JDK: 8, 11, 16" << std::endl;
        std::cout << "Python 2.7~3.10"
                     "之间的全部版本"
                  << std::endl;
        std::cout << "请输入你的开发环境以及版本号, 不同开发环境之间用回车隔开, 以#符号作为结束符" << std::endl;
        std::cout << "e.g. \ngcc 7\njdk 11\npython 2.9\n";

        while (1)
        {

            std::string instInfo;
            std::getline(std::cin, instInfo);

            envNvers = strSplit(instInfo, " "); // 分离开发环境名和版本号
            if (envNvers.size() < 2)
            {

                if (instInfo == "#")
                {
                    break;
                }
                std::cout << "输入不合法" << std::endl;
                continue;
            }
            envName = envNvers[0];
            envVersion = envNvers[1];
            // std::cout << envNvers[0] << std::endl << envNvers[1];
            devEnv.insert(devEnv.end(), envName);
            devVers.insert(devVers.end(), envVersion);
        }

        /* devEnv中保存各种的开发环境名, devVers中保存各种版本号 */

        while (!devVers.empty())
        {
            /* 取每一个版本号 */
            std::string devName, devVer;
            /* 每一个开发环境, 每一个版本 */
            devName = devEnv.back();
            devEnv.pop_back();
            devVer = devVers.back();
            devVers.pop_back();
            envset(devName, devVer);
        }

        return SUCCESS;
    }

    void getsaveinfo(std::string allinfo, std::string specinfo)
    {
        int index = -1;
        if (allinfo.find(specinfo, 0) != std::string::npos)
        {
            index = allinfo.find(specinfo, 0);
            std::cout << specinfo << " " << allinfo.substr(index+1+specinfo.length()) << std::endl;
        }
        return;
    }
}
